package com.hrm.salarys.service;


import com.hrm.common.util.IdWorker;
import com.hrm.domain.attendance.entity.ArchiveMonthlyInfo;
import com.hrm.domain.salarys.SalaryArchive;
import com.hrm.domain.salarys.SalaryArchiveDetail;
import com.hrm.domain.salarys.Settings;
import com.hrm.domain.salarys.UserSalary;
import com.hrm.domain.social_security.ArchiveDetail;
import com.hrm.salarys.client.FeignClientService;
import com.hrm.salarys.dao.SalaryArchiveDao;
import com.hrm.salarys.dao.SalaryArchiveDetailDao;
import com.hrm.salarys.dao.UserSalaryDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class ArchiveService {
    @Autowired
    private SalaryArchiveDao salaryArchiveDao;
    @Autowired
    private SalaryArchiveDetailDao salaryArchiveDetailDao;
    @Autowired
    private IdWorker idWorker;
    @Autowired
    private FeignClientService feignClientService;
    @Autowired
    private SettingsService settingsService;
    @Autowired
    private UserSalaryDao userSalaryDao;

    public SalaryArchive findSalaryArchive(String yearMonth, String companyId) {
        return salaryArchiveDao.finbByCompanyIdAndYearsMonth(companyId,yearMonth);
    }

    public List<SalaryArchiveDetail> findSalaryArchiveDetail(String id) {
        return salaryArchiveDetailDao.findByArchiveId(id);
    }

    public List<SalaryArchiveDetail> getReports(String yearMonth, String companyId) {
        //要计算薪资：前提是社保和考勤已经归档
        List<SalaryArchiveDetail> list = new ArrayList<>();
        //查询所有用户
        Page<Map> page = userSalaryDao.findPage(companyId, null); //null则查询所有
        //查询当前企业的福利津贴
        Settings settings = settingsService.findById(companyId);
        //遍历用户数据
        for (Map map : page.getContent()) {
            //构造salaryArchiveDetail
            SalaryArchiveDetail detail = new SalaryArchiveDetail();
            detail.setUser(map);
            //获取每个用户的社保数据
            ArchiveDetail info = feignClientService.getSocialInfo(detail.getUserId(), yearMonth);
            detail.setSocialInfo(info);
            //获取每个用户的考勤数据
            ArchiveMonthlyInfo atteInfo = feignClientService.getAtteInfo(detail.getUserId(), yearMonth);
            detail.setAtteInfo(atteInfo);
            //获取每个用户的薪资
            UserSalary userSalary = userSalaryDao.findById(detail.getUserId()).get();
            detail.setUserSalary(userSalary);
            //计算工资
            /*-----------  未完成   ------------*/
            list.add(detail);
        }
        return list;
    }

    public List<SalaryArchive> findAllSalaryArchive(String companyId) {
        return salaryArchiveDao.findByCompanyId(companyId);
    }
}
